<template>
  <div class="app-main-container">
    <!-- <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @handleQuery="getList(1)"
      @clear="clearFilters"
    /> -->
    <!-- <div v-if="meta.indexOf('SmsTemplateAdd') > -1" class="doumee-table-buttons"> -->
    <div class="doumee-table-buttons df_ac">
      <el-button
        v-if="meta.indexOf('SmsTemplateAdd') > -1"
        type="primary"
        @click="handleEdit()"
      >{{ translate("templateAdd") }}</el-button>
      <span
        class="ml20"
      >{{ translate("venueSyssig") }}：[{{ shopInfo.smsSign || "" }}]</span>
    </div>
    <el-table
      ref="table"
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table"
    >
      <el-table-column
        align="center"
        :label="translate('templateName')"
        prop="title"
        min-width="140"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="translate('sysType')"
        prop="smsType"
        min-width="100"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span v-if="scope.row.smsType === '0'">{{
            translate("noticeSms")
          }}</span>
          <span v-if="scope.row.smsType === '1'">{{ translate("yxSms") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="translate('status')"
        prop="creatertor"
        min-width="100"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span v-if="scope.row.smsStatus === '1'" class="success">{{
            translate("checkScs")
          }}</span>
          <span v-if="scope.row.smsStatus === '0'" class="padding">{{
            translate("checking")
          }}</span>
          <span v-if="scope.row.smsStatus === '2'" class="error">{{
            translate("checkErr")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="translate('sysContent')"
        prop="template"
        min-width="320"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="translate('sendFlag')"
        prop="smsEnable"
        width="100"
        fixed="right"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.smsEnable"
            :disabled="meta.indexOf('SmsTemplateUpdateSmsEnable') === -1"
            active-value="0"
            inactive-value="1"
            @change="changeSend(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column
        :label="translate('handle')"
        align="center"
        width="140"
        fixed="right"
      >
        <template slot-scope="scope">
          <div class="flex-cc">
            <!-- <el-button v-if="meta.indexOf('NoviceHelpEdit') > -1" v-preventReClick type="text" @click="handleEdit(scope.row)">{{translate('edit')}}</el-button> -->
            <el-button
              v-if="
                meta.indexOf('SmsTemplateAdd') > -1 && scope.row.smsType === '1'
              "
              v-preventReClick
              type="text"
              @click="handleEdit(scope.row)"
            >{{ translate("edit") }}</el-button>
            <el-button
              v-if="
                meta.indexOf('SmsTemplateDel') > -1 && scope.row.smsType === '1'
              "
              v-preventReClick
              type="text"
              style="color: red"
              @click="handleDel(scope.row.id)"
            >{{ translate("del") }}</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <Paginations
      :total="totalCount"
      :page="pagination.page"
      @current-change="getList"
      @SizeChange="handleSizeChange"
    />
    <Edit
      v-if="isShowEdit"
      ref="EditRef"
      @success="getList()"
      @close="isShowEdit = false"
    />
  </div>
</template>

<script>
import {
  smsTemplateList,
  smsTemplateDel,
  smsTemplateSendFlag,
  smsSignature
} from '@/api'
import { parseTime } from '@/utils/index'
import { queryFormConfig } from './config'

import Edit from './edit.vue'
export default {
  name: 'SMSTemplate',
  components: {
    Edit
  },
  data() {
    return {
      queryFormConfig,
      cgList: [],
      pagination: {
        page: 1,
        rows: 10
      },
      list: [],
      totalCount: 0,
      listLoading: false,
      meta: [],
      filters: {},
      shopInfo: {},

      isShowEdit: false
    }
  },
  created() {
    this.meta = this.$route.meta.buttons || []
    this.getList(1)
    this.getsmsSignature()
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  activated() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
    if (this.$route.query.renovate && this.$route.query.renovate === '1') {
      this.getList()
    }
  },
  methods: {
    changeSend(row) {
      smsTemplateSendFlag({
        param: {
          id: row.id,
          smsEnable: row.smsEnable
        }
      }).then(res => {
        this.$message.success(this.translate('updateScs'))
      }, () => {
        this.getList()
      })
    },
    getsmsSignature() {
      smsSignature({ param: {}}).then(res => {
        this.shopInfo = res.record
      })
    },
    handleEdit(row) {
      this.isShowEdit = true
      this.$nextTick(() => {
        this.$refs.EditRef.param = {
          smsType: '1'
        }
        if (row && row.id) {
          this.$refs.EditRef.param = { ...row }
        }
        this.$refs.EditRef.isShowModal = true
      })
    },
    handleDel(id) {
      const that = this
      this.$confirm(this.translate('tagDelPlace'), this.translate('ts'), {
        confirmButtonText: this.translate('qd'),
        cancelButtonText: this.translate('cancel'),
        type: 'warning'
      })
        .then(() => {
          smsTemplateDel({ param: { id }}).then((res) => {
            if (res.errorCode !== '000000') return
            that.$message.success(this.translate('delScs'))
            that.getList(1)
          })
        })
    },
    handleSizeChange(val) {
      this.pagination.rows = val
      this.getList(1)
    },
    getList(page) {
      const { pagination, filters: param } = this
      if (page) {
        pagination.page = page
        this.pagination.page = page
      }
      if (pagination.page === 1) {
        pagination.firstQueryTime = parseTime(new Date())
      }
      this.listLoading = true
      smsTemplateList({
        pagination,
        param
      }, sessionStorage.getItem('venueId')).then(res => {
        this.listLoading = false
        if (res.errorCode === '000000') {
          this.totalCount = res.totalCount
          this.list = res.recordList
        }
      }).catch(err => {
        this.listLoading = false
        console.log('-----', err)
      })
    },
    // 清空筛选项
    clearFilters() {
      this.filters = {}
      this.getList(1)
    }
  }
}
</script>

<style lang="scss" scoped>
.success {
  color: #80b536;
  background-color: #dae8c3;
  padding: 2px 4px;
  border-radius: 2px;
}
.padding {
  padding: 2px 4px;
  border-radius: 2px;
  color: #f2c998;
  background-color: #f7e2c6;
}
.error {
  padding: 2px 4px;
  border-radius: 2px;
  color: #e93322;
  background-color: #f8c1be;
}
</style>
